package com.iuie.ns.datum.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.iuie.ns.datum.mapper.DevProjectMapper;
import com.iuie.ns.datum.service.IDevProjectService;
import com.iuie.ns.datum.service.ITableStructureService;
import com.iuie.ns.datum.vo.DevProjectVO;
import com.iuie.ns.frame.exception.FileDownloadException;
import com.iuie.ns.frame.exception.ServiceException;
import com.iuie.ns.frame.utils.ListUtils;
import com.iuie.ns.frame.utils.ShiroUtils;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.system.vo.user.SysUserVO;

@Service("projectService")
public class DevProjectServiceImpl implements IDevProjectService {

	@Resource
	private DevProjectMapper devProjectMapper;
	
	@Resource
	private ITableStructureService tableStructureService;

	@Override
	public List<DevProjectVO> findByPage(Page page, DevProjectVO condition) {
		PageHelper.startPage(page.getPage(), page.getRows(), true);
		List<DevProjectVO> list = devProjectMapper.findByCondition(condition);
		page.loadData(list);
		return list;
	}

	@Override
	public void deleteById(Long id) {
		if (id == null) {
			throw new ServiceException("删除失败，ID为空！");
		}
		tableStructureService.deleteTableStructureByProjectId(id);
		devProjectMapper.deleteById(id);
	}

	@Override
	public void saveOne(DevProjectVO item) {
		if (StringUtils.isBlank(item.getCnProjectName())) {
			throw new ServiceException("保存失败，项目名称不能为空！");
		}
		if (StringUtils.isBlank(item.getEnProjectName())) {
			throw new ServiceException("保存失败，项目英文名称不能为空！");
		}
		if (!item.getEnProjectName().matches("^[a-zA-Z][a-zA-Z]{1,100}$")) {
			throw new ServiceException("保存失败，项目英文名称不合法，由26个英文字母和下划线组成！");
		}
		DevProjectVO mItem = this.findByEnName(item.getEnProjectName());
		if (mItem != null) {
			// 新增的情况下，不能与表中任意一行的enProjectName重复
			// 修改的情况下，如果enProjectName与原来相同，则忽略
			// 不同时，则根据ProjectName去查询，如果存在则不允许
			if (item.getId() == null || (item.getId() != null && item.getId().compareTo(mItem.getId()) != 0)) {
				throw new ServiceException("保存失败，项目英文名称与项目【" + mItem.getCnProjectName() + "】重复！");
			}
		}
		if (item.getId() == null) {
			// 新增
			SysUserVO user = (SysUserVO) ShiroUtils.getCurrentUser();
			String userId = user.getUserId();
			String userName = user.getUserName();
//			 String userId = "10000001";
//			 String userName = "刘杰";
			item.setCreatorId(userId);
			item.setCreatorName(userName);
			item.setCreateTime(new Date());
			item.setId(System.currentTimeMillis());
			devProjectMapper.addOne(item);
		} else {
			// 修改
			devProjectMapper.updateOne(item);
		}
	}

	@Override
	public DevProjectVO findById(Long id) {
		if (id == null) {
			return null;
		}
		return devProjectMapper.findById(id);
	}

	@Override
	public DevProjectVO findByEnName(String enProjectName) {
		if (StringUtils.isBlank(enProjectName)) {
			return null;
		}
		DevProjectVO condition = new DevProjectVO();
		condition.setEnProjectName(enProjectName);
		List<DevProjectVO> list = devProjectMapper.findByCondition(condition);
		if (ListUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<DevProjectVO> findAll() {
		return devProjectMapper.findByCondition(null);
	}
	
	@Override
	public void exportProjectTables(Long projectId, HttpServletResponse response) throws IOException {
		// 获取项目名称
		DevProjectVO project = devProjectMapper.findById(projectId);
		if (project == null || StringUtils.isBlank(project.getEnProjectName())) {
			throw new FileDownloadException("", "导出项目的表信息失败【project id is " + projectId + "】！");
		}
		String projectName = project.getEnProjectName();
		
		// 文件下载
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "form-data; name=\"attachment\";filename=\"TABLES_"+ projectName +".xlsx\"");
		OutputStream os = response.getOutputStream();
		ExportExcelUtils.exportProjectTablesExcel(project, os);
	}
	
	static class ExportExcelUtils {
		private static void exportProjectTablesExcel(DevProjectVO project, OutputStream os) {
			
		}
	}

	
	
}
